<?php
/**
 * Created by PhpStorm.
 * User: locove
 * Date: 15-9-15
 * Time: 上午10:25
 */
class checkpurchaseorder {
    /**
     * @param $puId
     * 插入采购单时去验证采购单是否复合条件（在生成采购单后执行）
     */
    public static function checkSinglePurchaseOrder($puId) {
        $puDetails = DB::select_array(array('id', 'supplier', DB::expr('UPPER(sku) AS `sku`'), 'quantity', 'single_price', 'money', 'transportation_expense'))
            ->from('purchaseorder_details')
            ->where('purchaseorder_id', '=', $puId)
            ->execute('newerp')->as_array('sku');
        $puMoney = DB::select(DB::expr('`money` + `transportation_expense` AS `money`'))->from('purchaseorders')->where('id', '=', $puId)->execute('newerp')->get('money', 0);
        if(!empty($puDetails)) {
            $skuArr = array_keys($puDetails);
            $skuPrices = Skusystemapi::getSkuArrPrice($skuArr);
            $sql = "SELECT UPPER(`sku`) AS `sku`,`daysales21` FROM `product_attached` WHERE `sku` IN ('".implode("','", $skuArr)."')";
            $sku21Sales = DB::query(Database::SELECT, $sql)->execute('readonlyerp')->as_array('sku', 'daysales21');

            $toplimitSql = "SELECT `table`,`value` FROM `statusdics` WHERE `colum_name` = 'toplimit'";
            $toplimits = DB::query(Database::SELECT, $toplimitSql)->execute('newerp')->as_array('table', 'value');

            //majunhong
            $a=join("','",$skuArr);
            $sql = "SELECT `sku` FROM `purchaseorder_details`
                    WHERE `sku` IN ('$a')
                    AND `status` != 100
                    AND `purchaseorder_id` != $puId";
            $newskuarr=DB::query(Database::SELECT, $sql)->execute('newerp')->as_array(NULL,'sku');
            //majunhong

            foreach($skuArr as $sku) {
                $reason = '';
                $skuPrice = isset($skuPrices[$sku]) && $skuPrices[$sku] ? $skuPrices[$sku] : 0;
                $skuQuantity = isset($sku21Sales[$sku]) && $sku21Sales[$sku] ? $sku21Sales[$sku] : 0;

                if($puDetails[$sku]['single_price'] > $skuPrice) $reason .= empty($reason) ? '1' : ',1';
                if($puDetails[$sku]['quantity'] > $skuQuantity)$reason .= empty($reason) ? '2' : ',2';

                $sql = "SELECT pd.`sku`,pd.`supplier` FROM `purchaseorder_details` AS `pd`
                    JOIN `purchaseorders` AS `p` ON (`pd`.`purchaseorder_id` = `p`.`id`)
                    WHERE `pd`.`sku` = '{$sku}'
                    AND `p`.`status` != 100 AND `pd`.`status` != 100
                    ORDER BY `p`.`purchaseorder_date` DESC
                    LIMIT 1,10";

                if(!in_array($puDetails[$sku]['supplier'], DB::query(Database::SELECT, $sql)->execute('newerp')->as_array(NULL, 'supplier')))
                    $reason .= empty($reason) ? '3' : ',3';

                if($puMoney >= $toplimits['purchaseorders'])
                    $reason .= empty($reason) ? '4' : ',4';

                if(($puDetails[$sku]['money'] + $puDetails[$sku]['transportation_expense']) >= $toplimits['purchaseorder_details'])
                    $reason .= empty($reason) ? '5' : ',5';

                 //majunhong
                if(!in_array($puDetails[$sku]['sku'],$newskuarr)){
                    if (($puDetails[$sku]['money'] + $puDetails[$sku]['transportation_expense']) >= $toplimits['purchaseorder_details_newsku'])
                        $reason .= empty($reason) ? '6' : ',6';
                }
                //majunhong

                $sqlDetail = "UPDATE `purchaseorder_details` SET `prime_cost` = {$skuPrice}, `daysales14` = {$skuQuantity} ";
                if(!empty($reason)) {
                    $sql = "UPDATE `purchaseorders` SET `pass` = 1 WHERE `id` = ".$puId;
                    DB::query(Database::UPDATE, $sql)->execute('newerp');

                    $sqlDetail .= ", `reason` = '{$reason}'";
                }
                $sqlDetail .= " WHERE `id` = ".$puDetails[$sku]['id'];
                DB::query(Database::UPDATE, $sqlDetail)->execute('newerp');
            }
        }
    }
}